//
//  FMSManager.m
//  FMSManagerDemo
//
//  Created by hongbao.cui on 14-11-10.
//  Copyright (c) 2014年 hongbao.cui. All rights reserved.
//

#import "FMSManager.h"
#import "TestViewController.h"
#import "TestExerciseViewController.h"
#import "ApplicationUtils.h"
#import "ExerciseDetailViewController.h"
#import "MyCollectionViewController.h"
#import "MyMarkListViewController.h"
#import "Sqlite.h"
#import "FMBaseUINavgationViewController.h"
//#import "iflyMSC/iflySetting.h"
//#import "iflyMSC/IFlySpeechUtility.h"
//#import "IFlyFlowerCollector.h"
@interface FMSManager(){
}
@end
@implementation FMSManager
@synthesize backBlock;
+(FMSManager *)shareInstance{
    static FMSManager *sharedManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[FMSManager alloc] init];
//        //设置log等级，此处log为默认在app沙盒目录下的msc.log文件
//        [IFlySetting setLogFile:LVL_ALL];
//        //输出在console的log开关
//        [IFlySetting showLogcat:YES];
//        
//        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
//        NSString *cachePath = [paths objectAtIndex:0];
//        //设置msc.log的保存路径
//        [IFlySetting setLogFilePath:cachePath];
//        //创建语音配置,appid必须要传入，仅执行一次则可
//        NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@,timeout=%@",APPID_VALUE,TIMEOUT_VALUE];
//        //所有服务启动前，需要确保执行createUtility
//        [IFlySpeechUtility createUtility:initString];
//        [IFlyFlowerCollector SetDebugMode:YES];
//        [IFlyFlowerCollector SetCaptureUncaughtException:YES];
//        [IFlyFlowerCollector SetAppid:@"test2014"];
//        [IFlyFlowerCollector SetAutoLocation:YES];
        
    });
    return sharedManager;
}
-(void)setUserInfo:(NSString *)user_author{
    NSUserDefaults *standords = [NSUserDefaults standardUserDefaults];
    [standords setObject:user_author forKey:@"userName"];
    [standords synchronize];
}
-(TestViewController *)showTestVC:(NSString *)authToken paperID:(NSString *)paperId{
    NSString  *plistPath = [[NSBundle mainBundle] pathForResource:@"Config" ofType:@"plist"];
    NSMutableDictionary *dataDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSString *ip_host = [dataDict objectForKey:@"ip_host"];
    NSString *ip_root = [dataDict objectForKey:@"ip_root"];
    if (!ip_host||!ip_root) {
        FMSCHBLog(@"ip_host  is  null");
        return nil;
    }
    NSString *ip_hostAll = [NSString stringWithFormat:@"%@%@/getPaperData.do?paperId=%@&authToken=%@",ip_host,ip_root,paperId,authToken];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:ip_hostAll,@"paperFileURL",paperId,@"paperID", nil];
    TestViewController *testCtrller = [[TestViewController alloc] initWithNibName:nil bundle:nil];
    testCtrller.urlDict = dict;
    return testCtrller;
}
-(void)showModelPaper:(NSString *)authToken paperID:(NSString *)paperId Model:(NSString *)Model{
    NSString  *plistPath = [[NSBundle mainBundle] pathForResource:@"Config" ofType:@"plist"];
    NSMutableDictionary *dataDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSString *ip_host = [dataDict objectForKey:@"ip_host"];
    NSString *ip_root = [dataDict objectForKey:@"ip_root"];
    NSString *sqliteVersion_New = [dataDict objectForKey:@"sqliteVersion"];
    if (!ip_host||!ip_root) {
        FMSCHBLog(@"ip_host  is  null");
        return;
    }
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *sqliteVersion_Old = [defaults objectForKey:@"sqliteVersion"];
    if (sqliteVersion_Old==nil||![sqliteVersion_New isEqualToString:sqliteVersion_Old]) {
        [self clearCache];
    }
    [defaults setObject:sqliteVersion_New forKey:@"sqliteVersion"];
    NSString *ip_hostAll = [NSString stringWithFormat:@"%@%@/getPaperData.do?paperId=%@&authToken=%@",ip_host,ip_root,paperId,authToken];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:ip_hostAll,@"paperFileURL",paperId,@"paperID", nil];
    FMBaseUINavgationViewController *navCtrll = nil;
    if ([Model isEqualToString:@"0"]) {
        //试题测试
        TestViewController *testCtrller = [[TestViewController alloc] initWithNibName:nil bundle:nil];
        testCtrller.offLineOrNot = _offLineOrNot;
        testCtrller.urlDict = dict;
        __weak FMSManager *weakSelf = self;
        testCtrller.testVCBackBlock = ^(TestRootViewController *test){
            dispatch_async(dispatch_get_main_queue(), ^{
                if (weakSelf.backBlock) {
                    weakSelf.backBlock(nil);
                }
            });

        };
       navCtrll = [[FMBaseUINavgationViewController alloc] initWithRootViewController:testCtrller];
        [navCtrll setNavigationBarBg];
        [_fmsCtrll presentViewController:navCtrll animated:YES completion:^{
            
        }];
    }else{
        //练习模式
        TestExerciseViewController *testCtrller = [[TestExerciseViewController alloc] initWithNibName:nil bundle:nil];
        testCtrller.urlDict = dict;
        testCtrller.backBlock = ^(){
            
        };
        navCtrll = [[FMBaseUINavgationViewController alloc] initWithRootViewController:testCtrller];
        [navCtrll setNavigationBarBg];
        [_fmsCtrll presentViewController:navCtrll animated:YES completion:^{
            
        }];
    }
}
-(void)showSynExercise:(NSString *)authToken paperID:(NSString *)paperId{
    NSString  *plistPath = [[NSBundle mainBundle] pathForResource:@"Config" ofType:@"plist"];
    NSMutableDictionary *dataDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSString *ip_host = [dataDict objectForKey:@"ip_host"];
    NSString *ip_root = [dataDict objectForKey:@"ip_root"];
    if (!ip_host||!ip_root) {
        FMSCHBLog(@"ip_host  is  null");
        return;
    }
    NSString *ip_hostAll = [NSString stringWithFormat:@"%@%@/getPaperData.do?paperId=%@&authToken=%@",ip_host,ip_root,paperId,authToken];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:ip_hostAll,@"exerciseFileURL",paperId,@"exerciseID", nil];
    ExerciseDetailViewController *detail = [[ExerciseDetailViewController alloc] initWithNibName:nil bundle:nil];
    FMBaseUINavgationViewController *navCtrll = [[FMBaseUINavgationViewController alloc] initWithRootViewController:detail];
    [navCtrll setNavigationBarBg];
    detail.urlDict = dict;
    [_fmsCtrll presentViewController:navCtrll animated:YES completion:^{
        
    }];
}
-(void)showCollection{
    MyCollectionViewController *myCollection = [[MyCollectionViewController alloc] initWithNibName:nil bundle:nil];
    FMBaseUINavgationViewController *navCtrll = [[FMBaseUINavgationViewController alloc] initWithRootViewController:myCollection];
    [navCtrll setNavigationBarBg];
    [_fmsCtrll presentViewController:navCtrll animated:YES completion:^{
        
    }];
}
-(BOOL)clearMyCollection{
   BOOL  sucess = [Sqlite deleteAllCollectionQuestion];
    if (sucess) {
        return YES;
    }
    return NO;
}
-(void)showAchivement{
    MyMarkListViewController *myMark = [[MyMarkListViewController alloc] initWithNibName:nil bundle:nil];
    FMBaseUINavgationViewController *navCtrll = [[FMBaseUINavgationViewController alloc] initWithRootViewController:myMark];
    [navCtrll setNavigationBarBg];
    [_fmsCtrll presentViewController:navCtrll animated:YES completion:^{
        
    }];
}
-(BOOL)clearMyAchivement{
    BOOL  sucess = [Sqlite deleteAllPaper];
    if (sucess) {
        return YES;
    }
    return NO;
}
-(void)clearCache{
    [NSUserDefaults resetStandardUserDefaults];
    NSUserDefaults *standors = [NSUserDefaults standardUserDefaults];
    [standors removeObjectForKey:@"userName"];
    
    NSDictionary *dictionary = [standors dictionaryRepresentation];
    for(NSString* key in [dictionary allKeys]){
        [standors removeObjectForKey:key];
        [standors synchronize];
    }
    
    NSString *path = FMPAPERSAVE_PATH;[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Paper"];
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSError *error = nil;
    BOOL success = [fileMgr removeItemAtPath:path error:&error];
    if (success) {
        FMSCHBLog(@"－－－－－－删除目录成功");
    }
}
@end
